package com.alphasta.project.system.ncp.service;
import com.alphasta.common.utils.GetToken;
import com.alphasta.common.utils.JsonUtil;
import com.alphasta.project.system.ncp.entity.NcpPersonInfoFrom;
import com.alphasta.project.system.ncp.entity.NcpPersonInfoVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;


/**
 * 确诊和疑似病例数据 服务层 处理
 *
 * @author 孟凡超
 */
@Service
@Slf4j
public class NcpPersonInfoServiceImpl implements NcpPersonInfoService {

    @Value("${qqjg.qqjgdm}")
    private String qqjgdm;// 请求机构代码,
    @Value("${qqjg.qqjgmc}")
    private String qqjgmc;// 请求机构名称,
    @Value("${ncp.ncpPersonInfoUrl}")
    private String ncpPersonInfoUrl;
    @Value("${ncp.contentType}")
    private String contentType;
    @Value("${ncp.processNum}")
    private String processNum;
    @Value("${ncp.stepNum}")
    private String stepNum;
    @Value("${ncp.receiveAppId}")
    private String receiveAppId;
    @Value("${ncp.invokingAppId}")
    private String invokingAppId;
    @Value("${ncp.apiKey}")
    private String apiKey;
    @Value("${ncp.secretKey}")
    private String secretKey;
    @Value("${ncp.topicCode}")
    private String topicCode;
    @Override
    public NcpPersonInfoVO NcpPersonInfo(NcpPersonInfoFrom ncpPersonInfoFrom) {
        NcpPersonInfoVO ncpPersonInfoVO=new NcpPersonInfoVO();


      //  String url = "http://localhost:8000/ncp/ncpPersonInfo/list";
//headers
        HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.add("Content-Type", contentType);
        requestHeaders.add("processNum", processNum);
        requestHeaders.add("stepNum", stepNum);
        requestHeaders.add("receiveAppId", receiveAppId);
        requestHeaders.add("invokingAppId", invokingAppId);
        //获取token
       /* String token= GetToken.getToken();
        log.info("获取到的token="+token);
        requestHeaders.add("access_token", token);*/
//body
        MultiValueMap<String, String> requestBody = new LinkedMultiValueMap<>();
        requestBody.add("apiKey", apiKey);
        requestBody.add("secretKey", secretKey);
        requestBody.add("topicCode", topicCode);

        requestBody.add("cxsy", ncpPersonInfoFrom.getCxsy());
        requestBody.add("qqjgdm", qqjgdm);
        requestBody.add("qqjgmc", qqjgmc);
        requestBody.add("qqr", ncpPersonInfoFrom.getQqr());
        requestBody.add("qqrsfzhm", ncpPersonInfoFrom.getQqrsfzhm());
        requestBody.add("sfzhm", ncpPersonInfoFrom.getSfzhm());
        requestBody.add("xm", ncpPersonInfoFrom.getXm());
//HttpEntity
        HttpEntity<MultiValueMap<String,String>> requestEntity=new HttpEntity<MultiValueMap<String,String>>(requestBody, requestHeaders);
        RestTemplate restTemplate = new RestTemplate();
//post
        ResponseEntity<String> responseEntity = restTemplate.postForEntity(ncpPersonInfoUrl, requestEntity, String.class);
        ncpPersonInfoVO= JsonUtil.jsonToPojo(responseEntity.getBody(),NcpPersonInfoVO.class);
        return ncpPersonInfoVO;
    }
}
